package hanjia;

import java.util.LinkedList;

/**
 * Description:
 * Author:    Dane
 * CreateTime:2025/2/10-上午11:35
 * Since  :   1.0
 * Version:   1.0
 */
public class LeetCode209 {

    public static int minSubArrayLen2(int target, int[] nums) {
        LinkedList<Integer> deque = new LinkedList<>();
        int minLength=Integer.MAX_VALUE;
        int sum=0;
        for(int i:nums){
            sum+=i;
            deque.offerLast(i);
                while (sum>=target){
                    minLength=Math.min(minLength,deque.size());
                    Integer pre = deque.pollFirst();
                    sum-=pre;
                }




        }
        return minLength==Integer.MAX_VALUE?0:minLength;
    }

    public static void main(String[] args) {
       int  target = 7;
        int nums[] = {2,3,1,2,4,3};
        int i = minSubArrayLen2(target, nums);
        System.out.println(i);
    }


    public static int minSubArrayLen(int target, int[] nums) {
        int left = 0;
        int right = 0;
        int minLength = Integer.MAX_VALUE;
        int currentSum = 0;
        while (right<nums.length){
            currentSum+=nums[right];
            while (currentSum>=target)
            {
                minLength=Math.min(minLength,right-left+1);
                currentSum-=nums[left];
                left++;


            }

            right++;

        }
        minLength = minLength != Integer.MAX_VALUE ? minLength : 0;
        return minLength;


    }



}
